package org.system.core.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.system.core.annotation.Log;
import org.system.core.common.Result;
import org.system.core.domain.entity.SysDept;
import org.system.core.enums.BusinessType;
import org.system.core.service.system.SysDeptService;

import java.util.List;

/**
 * @author baoRoot
 */
@Api(tags={"部门模块"})
@RestController
@RequestMapping("/system/dept")
public class SysDeptController {

    private static final Logger logger = LoggerFactory.getLogger(SysDeptController.class);

    @Autowired
    private SysDeptService sysDeptService;

    @ApiOperation("获取部门列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyPermi('system:dept:list')")
    public Result<List<SysDept>> list(SysDept sysDept) {
        logger.info("获取部门列表， 查询参数： {}", sysDept);
        List<SysDept> deptList = sysDeptService.selectDeptList(sysDept);
        return Result.ok(deptList);
    }

    @ApiOperation("获取部门详细数据")
    @GetMapping("/{deptId}")
    @PreAuthorize("@ss.hasAnyPermi('system:dept:query')")
    public Result<SysDept> getDept(@PathVariable Long deptId) {
        logger.info("查询ID为：{} 的部门详细数据", deptId);
        sysDeptService.checkDeptDataScope(deptId);
        return Result.ok(sysDeptService.selectDeptById(deptId));
    }

    @ApiOperation("新增部门")
    @PostMapping
    @PreAuthorize("@ss.hasAnyPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    public Result<String> addDept(@Validated @RequestBody SysDept dept) {
        logger.info("新增【{}】部门", dept);
        sysDeptService.insertDept(dept);
        return Result.ok();
    }

    @ApiOperation("更新部门")
    @PutMapping
    @Transactional
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    public Result<String> editDept(@Validated @RequestBody SysDept dept){
        logger.info("修改【{}】部门", dept);
        sysDeptService.updateDept(dept);
        return Result.ok();
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{deptId}")
    @PreAuthorize("@ss.hasPermi('system:dept:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    public Result<String> deleteDept(@PathVariable Long deptId) {
        logger.info("删除id为：{} 的部门", deptId);
        sysDeptService.deleteDept(deptId);
        return Result.ok();
    }

}
